/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.extension.db.unit;

import static java.sql.Statement.KEEP_CURRENT_RESULT;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mule.extension.db.internal.result.statement.StatementResultIterator.NO_UPDATE_COUNT;

import org.mule.extension.db.internal.domain.autogeneratedkey.AutoGenerateKeysStrategy;
import org.mule.extension.db.internal.domain.autogeneratedkey.DefaultAutoGenerateKeysStrategy;
import org.mule.extension.db.internal.domain.connection.DbConnection;
import org.mule.extension.db.internal.domain.param.DefaultOutputQueryParam;
import org.mule.extension.db.internal.domain.query.QueryTemplate;
import org.mule.extension.db.internal.domain.type.DbType;
import org.mule.extension.db.internal.result.resultset.ResultSetHandler;
import org.mule.extension.db.internal.result.statement.StatementResultIterator;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Before;
import org.junit.Test;

public class StatementResultIteratorTestCase {

  private StatementResultIterator statementResultIterator;

  @Before
  public void beforeTest() throws SQLException {
    AutoGenerateKeysStrategy autoGenerateKeysStrategy = new DefaultAutoGenerateKeysStrategy();

    DbConnection dbConnection = mock(DbConnection.class);
    Connection connection = mock(Connection.class);
    DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    when(dbConnection.getJdbcConnection()).thenReturn(connection);
    when(connection.getMetaData()).thenReturn(databaseMetaData);
    when(databaseMetaData.supportsMultipleOpenResults()).thenReturn(true);

    CallableStatement statement = mock(CallableStatement.class);
    ResultSet resultSet = mock(ResultSet.class);
    when(statement.getResultSet()).thenReturn(resultSet);
    when(statement.getGeneratedKeys()).thenReturn(null);
    when(statement.getMoreResults(KEEP_CURRENT_RESULT)).thenReturn(false);
    when(statement.getUpdateCount()).thenReturn(NO_UPDATE_COUNT);

    QueryTemplate queryTemplate = mock(QueryTemplate.class);
    when(queryTemplate.getOutputParams()).thenReturn(emptyList());

    ResultSetHandler resultSetHandler = mock(ResultSetHandler.class);
    when(resultSetHandler.processResultSet(dbConnection, resultSet)).thenReturn(null);

    statementResultIterator =
        new StatementResultIterator(dbConnection, statement, queryTemplate, autoGenerateKeysStrategy, resultSetHandler);
  }

  @Test
  public void setStatementResultIteratorHastNextReturnsFalseWhereThereIsNoMoreResultSets() {
    assertTrue(statementResultIterator.hasNext());
    statementResultIterator.next();
    assertFalse(statementResultIterator.hasNext());

  }

}
